package com.example.myxmpp.activity;

import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.myxmpp.R;
import com.example.myxmpp.fragment.ContactsFragment;
import com.example.myxmpp.fragment.SessionFragment;
import com.example.myxmpp.service.IMService;
import com.example.myxmpp.utils.ToolBarUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

    //@BindView(R.id.main_bottom)
   // LinearLayout mMainBottom;

   // @BindView(R.id.main_tv_title)
   // TextView mMainTvTitle;

//    @BindView(R.id.main_viewpager)
//    ViewPager mMainViewpager;


    private List<Fragment> mFragments = new ArrayList<Fragment>();

    private ViewPager mMainViewPager;
    private LinearLayout mMainBottom;
    private TextView mMainTvTitl;
    private ToolBarUtil toolBarUtil;
    private String[] toolBarTitleArr;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //ButterKnife.bind(this);
        initView();

        initData();

        initListener();


    }

    private void initListener() {

        mMainViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                //修改
                toolBarUtil.changeColor(position);
                //修改title
                mMainTvTitl.setText(toolBarTitleArr[position]);

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        toolBarUtil.setmOnToolBarClickListener(new ToolBarUtil.OnToolBarClickListener() {
            @Override
            public void onToolBarClick(int position) {
                mMainViewPager.setCurrentItem(position);
            }
        });
    }

    private void initView() {
        mMainViewPager = findViewById(R.id.main_viewpager);
        mMainBottom = findViewById(R.id.main_bottom);
        mMainTvTitl = findViewById(R.id.main_tv_title);
    }

    private void initData() {

        //添加Fragment到集合中
        mFragments.add(new SessionFragment());
        mFragments.add(new ContactsFragment());


        mMainViewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));

        //底部按钮
        toolBarUtil = new ToolBarUtil();
        //文字内容
        toolBarTitleArr = new String[]{"会话","联系人"};
        //图标内容
        int[] iconArr = {R.drawable.selector_meassage,R.drawable.selector_selfinfo};

        toolBarUtil.createToolBar(mMainBottom, toolBarTitleArr,iconArr);

        //设置默认选中会话
        toolBarUtil.changeColor(0);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    class MyPagerAdapter extends FragmentPagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }
    }
}
